package com.easyadmin.modules.job.schedule;

import com.easyadmin.common.quartz.base.BaseQuartz;
import com.easyadmin.modules.job.domain.ScheduleLog;
import com.easyadmin.modules.job.mapper.ScheduleLogMapper;
import com.easyadmin.modules.sys.mapper.SysLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


@Slf4j
@Component("deleteLogs")
public class DeleteLogs implements BaseQuartz {


    @Autowired
    private ScheduleLogMapper scheduleLogMapper;
    @Autowired
    private SysLogMapper sysLogMapper;
    @Autowired
    ThreadPoolExecutor executor;


    @Override
    public void run(String params) throws Exception {

        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 转换为字符串
        String todayStr = today.format(formatter);
        //判断当日数据是否执行
        List<ScheduleLog> scheduledata= scheduleLogMapper.selectCreateTime(todayStr,"deleteLogs");
        if(scheduledata.size()>0){
            return;
        }


        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            scheduleLogMapper.deleteCreteTime();
        }, executor);
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            sysLogMapper.deleteCreteTime();
        }, executor);
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2);
        log.info(" 开始删除日志完成   DeleteLogs ");

    }



}
